package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.glowmee.cmd.PouchCMD;
import com.glowdayz.glowmee.dao.PouchDAO;

public class ListEditerPickBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public ListEditerPickBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String insertEditerPouch_str = request.getParameter("insertEditerPouch");
		String insertStart_date = request.getParameter("insertStart_date");
		String insertEnd_date = request.getParameter("insertEnd_date");
		if (insertEditerPouch_str != null && insertStart_date != null && insertEnd_date != null) {
			int insertEditerPouch = Integer.parseInt(insertEditerPouch_str);
			insertStart_date = insertStart_date.replace("-", "").replace("T", "").replace(":", "") + "00";
			insertEnd_date = insertEnd_date.replace("-", "").replace("T", "").replace(":", "") + "00";
			try {
				PouchDAO.getInstance().insertEditerPick(new PouchCMD(insertEditerPouch, insertStart_date, insertEnd_date));
				request.setAttribute("alert", "<script type='text/javascript'>alert('등록이 완료되었습니다.')</script>");
			} catch (SQLException e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String[] delete_list = request.getParameterValues("delete_list");
		if (delete_list != null) {
			try {
				PouchDAO.getInstance().deleteEditerPick(delete_list);
				request.setAttribute("alert", "<script type='text/javascript'>alert('삭제가 완료되었습니다.')</script>");
			} catch (SQLException e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("pouch", PouchDAO.getInstance().getPouchList(1, -1, "p.pouchTitle", " AND p.editerPick=0"));
			request.setAttribute("editerPick", PouchDAO.getInstance().getEditerPickList(page, 10));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/listEditerPick.jsp");
		dpr.forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String id_str = request.getParameter("id");
		String editStart_date = request.getParameter("start_date" + id_str);
		String editEnd_date = request.getParameter("end_date" + id_str);
		
		if (id_str != null && editStart_date != null && editEnd_date != null) {
			int id = Integer.parseInt(id_str);
			editStart_date = editStart_date.replace("-", "").replace("T", "").replace(":", "") + "00";
			editEnd_date = editEnd_date.replace("-", "").replace("T", "").replace(":", "") + "00";
			
			try {
				PouchDAO.getInstance().updateEditerPick(new PouchCMD(id, editStart_date, editEnd_date));
				request.setAttribute("alert", "<script type='text/javascript'>alert('수정이 완료되었습니다.')</script>");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("pouch", PouchDAO.getInstance().getEditerPouchList(1, -1, "p.pouchTitle", " AND p.editerPick=0"));
			request.setAttribute("editerPick", PouchDAO.getInstance().getEditerPickList(page, 10));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/listEditerPick.jsp");
		dpr.forward(request, response);
	}

}